package com.snack.shop.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.snack.shop.common.Result;
import com.snack.shop.entity.SystemConfig;
import com.snack.shop.service.SystemConfigService;
import com.snack.shop.entity.PickupCategory;
import com.snack.shop.service.PickupCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;

/**
 * 系统配置控制器
 */
@Tag(name = "系统配置管理")
@RestController
@RequestMapping("/config")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;
    private final PickupCategoryService pickupCategoryService;

    @Operation(summary = "获取单个配置")
    @GetMapping("/get")
    public Result<String> getConfig(
        @Parameter(description = "配置键") @RequestParam String key
    ) {
        String value = systemConfigService.getConfigValue(key);
        return Result.success(value);
    }

    @Operation(summary = "获取所有配置")
    @GetMapping("/all")
    public Result<List<SystemConfig>> getAllConfigs() {
        List<SystemConfig> configs = systemConfigService.getAllConfigs();
        return Result.success(configs);
    }

    @Operation(summary = "获取公开配置（平台名称，小程序初始化时调用）")
    @GetMapping("/public")
    public Result<Map<String, String>> getPublicConfigs() {
        Map<String, String> publicConfigs = new HashMap<>();
        publicConfigs.put("platformName", systemConfigService.getConfigValue("platform_name"));
        return Result.success(publicConfigs);
    }

    @Operation(summary = "获取零食店配置（配送费、起送金额等）")
    @GetMapping("/snack")
    public Result<Map<String, String>> getSnackConfigs() {
        Map<String, String> snackConfigs = new HashMap<>();
        snackConfigs.put("deliveryFee", systemConfigService.getConfigValue("delivery_fee"));
        snackConfigs.put("minOrderAmount", systemConfigService.getConfigValue("min_order_amount"));
        return Result.success(snackConfigs);
    }

    @Operation(summary = "获取首页聚合配置（公告、轮播图等）")
    @GetMapping("/home")
    public Result<Map<String, Object>> getHomeConfigs() {
        Map<String, Object> homeConfigs = new HashMap<>();
        homeConfigs.put("announcement", systemConfigService.getConfigValue("home_announcement"));
        homeConfigs.put("bannersConfig", systemConfigService.getConfigValue("home_banners_config"));
        return Result.success(homeConfigs);
    }

    @Operation(summary = "更新单个配置（商家）")
    @SaCheckRole("merchant")
    @PostMapping("/update")
    public Result<Void> updateConfig(
        @Parameter(description = "配置键") @RequestParam String key,
        @Parameter(description = "配置值") @RequestParam String value
    ) {
        systemConfigService.updateConfig(key, value);
        return Result.success();
    }

    @Operation(summary = "批量更新配置（商家）")
    @SaCheckRole("merchant")
    @PostMapping("/batch")
    public Result<Void> batchUpdateConfigs(@RequestBody List<SystemConfig> configs) {
        systemConfigService.batchUpdateConfigs(configs);
        return Result.success();
    }

    @Operation(summary = "获取代取分类基础价（按服务类型）")
    @GetMapping("/pickup/base-price")
    public Result<BigDecimal> getPickupBasePrice(
        @Parameter(description = "服务类型：2-取快递，3-取外卖，4-代取奶咖") @RequestParam Integer serviceType
    ) {
        PickupCategory category = pickupCategoryService.getByServiceType(serviceType);
        BigDecimal price = category != null && category.getBasePrice() != null ? category.getBasePrice() : BigDecimal.ZERO;
        return Result.success(price);
    }

    @Operation(summary = "更新代取分类基础价（商家，按服务类型）")
    @SaCheckRole("merchant")
    @PostMapping("/pickup/base-price")
    public Result<Void> updatePickupBasePrice(
        @Parameter(description = "服务类型：2-取快递，3-取外卖，4-代取奶咖") @RequestParam Integer serviceType,
        @Parameter(description = "基础价格") @RequestParam BigDecimal basePrice
    ) {
        pickupCategoryService.updateBasePriceByServiceType(serviceType, basePrice);
        return Result.success();
    }
}

